home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Collections: Various
/
DevDisk 65 (1989)(DevWare PD).zip
/
DevDisk 65 (1989)(DevWare PD).adf
/
trapsnap
/
Trap.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-07-11
|
3KB
|
95 lines
/** TrapSnapper From The Transactor
*
* This program shows an easy way to handle CPU exceptions from
* a C program. Just include the structs below, and put your error
* message and/or cleanup code in the function MyHandler(). You can
* get the trap number as shown in this example.
*
* (c) 1987 AHA!
**/
#include <exec/tasks.h>
struct Task *MyTask, *FindTask();
struct TrapData {
long TrapNum; /* trap number will be stored here */
int (*Code)(); /* pointer to user trap handler function */
USHORT MLcode[14]; /* trap-handler machine code goes here */
};
struct TrapData MyTrap = {
0,
NULL, /* trap code address will be filled in later */
0x201F, /* MOVE.L (A7)+,D0 */
0x41FA, 0xFFF4, /* LEA *-$A,A0 */
0x2080, /* MOVE.L D0,(A0) */
0xB0BC, 0x0000, 0x0003, /* CMP.L #3,D0 */
0x6202, /* BHI.S *+4 */
0x504F, /* ADDQ.W #8,A7 */
0x41FA, 0xFFE8, /* LEA *-$16,A0 */
0x2F50, 0x0002, /* MOVE.L (A0),2(A7) */
0x4E73 /* RTE */
};
/* commented assembler code is below:
*
* errnum: DS.L 1
* errfunc: DS.L 1
* start: MOVE.L (A7)+,D0 ;pull trap number off stack
* LEA errnum,A0 ;force PC-relative addr mode
* MOVE.L D0,(A0) ;put trap # in errnum for C
* CMP.L #3,D0 ;check for bus or address error
* BHI other ;other traps (4 or greater)
* ADDQ #8,A7 ;skip extra info on stack
* other: LEA errfunc,A0 ;PC rel addressing
* MOVE.L (A0),2(A7) ;point PC on stack to user routine
* RTE ;exit from trap-handler
*/
extern int MyHandler(); /* your trap-handling function */
main (argc, argv)
int argc;
char *argv[];
{
int n1, n2;
int i2, *i1;
i1 = 1;
SetTrap(); /* set up trap handler */
if (argc != 2)
{
printf("Usage: %s <n>\n", argv[0]);
exit (0);
}
n1 = atoi(argv[1]); /* value from command line */
/* if arg=1 let's get an address error by referencing an odd address */
if (n1 == 1)
i2 = *i1;
/* if n1 is zero, we'll get a divide by zero trap */
n2 = 100 / n1;
printf("100 divided by %d equals %d\n", n1, n2);
}
SetTrap ()
{
MyTask = FindTask(0L);
MyTask->tc_TrapCode = (APTR)MyTrap.MLcode;
MyTrap.Code = MyHandler;
}
MyHandler ()
{
printf("Hey - watch it! If I weren't a sophisticated\n");
printf("Transactor program, I would have bombed out!\n");
printf("(The problem was, I got a trap number %ld)\n", MyTrap.TrapNum);
exit(0);
}